import { Guard, IGuard, getPropertyMetadata } from '@midwayjs/core';
import { Context } from '@midwayjs/koa';


@Guard()
export class AuthGuard implements IGuard<Context> {
  async canActivate(context: Context, supplierClz: any, methodName: string): Promise<boolean> {
    // 从类元数据上获取角色信息
    const roleNameList = getPropertyMetadata<string[]>('role:name', supplierClz, methodName);
    if (roleNameList && roleNameList.length && context.user.role) {
      return roleNameList.includes(context.user.role);
    }
    return false;
  }
}
